package com.bawei.lib_common.view

import android.content.Context
import android.graphics.*
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import java.util.*

/**
 *author：yjk
 *description:
 *date：2025/6/11
*/
class PunchClockView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    // 颜色配置
    private val blueColor = Color.parseColor("#4285F4") // 蓝色
    private val grayColor = Color.parseColor("#F1F1F1")   // 浅灰
    private val whiteColor = Color.WHITE                 // 白色
    private val textColor = Color.BLACK                  // 黑色文字

    // 画笔配置
    private val outerRingPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = 16f.dpToPx()
    }

    private val innerRingPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = 16f.dpToPx()
    }

    private val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
        color = whiteColor
    }

    private val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
        color = textColor
        textSize = 16f.spToPx()
        textAlign = Paint.Align.CENTER
    }

    private val timePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.GRAY
        textSize = 12f.spToPx()
        textAlign = Paint.Align.CENTER
    }

    // 时间判断逻辑
    private fun getTimeInfo(): Triple<String, String, Boolean> {
        val currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
        return if (currentHour < 12) {

            Triple("上午打卡", "08:00-12:00", true) // 上午：蓝色外环
        } else {

            Triple("下午打卡", "13:30-18:00", false) // 下午：蓝色内环
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val centerX = width / 2f
        val centerY = height / 2f
        val radius = (minOf(width, height) / 2f) * 0.8f

        // 获取时间信息（包含是否为上午的标志）
        val (title, timeRange, isMorning) = getTimeInfo()

        // 设置圆环颜色（根据上午/下午切换）
        if (isMorning) {
            outerRingPaint.color = blueColor
            innerRingPaint.color = grayColor
        } else {
            outerRingPaint.color = grayColor
            innerRingPaint.color = blueColor
        }

        // 1. 绘制白色背景圆
        canvas.drawCircle(centerX, centerY, radius, bgPaint)

        // 2. 绘制双层圆环（颜色已根据时间设置）
        canvas.drawCircle(centerX, centerY, radius, outerRingPaint) // 外环
        canvas.drawCircle(centerX, centerY, radius * 0.8f, innerRingPaint) // 内环

        // 3. 绘制主文本
        val textBaselineY = centerY - (textPaint.descent() + textPaint.ascent()) / 2
        canvas.drawText(title, centerX, textBaselineY, textPaint)

        // 4. 绘制时间范围
        val timeY = textBaselineY + timePaint.textSize * 1.5f
        canvas.drawText(timeRange, centerX, timeY, timePaint)
    }

    // 刷新显示（可用于外部主动刷新）
    fun refresh() {
        invalidate()
    }

    // 单位转换扩展
    private fun Float.dpToPx(): Float = this * resources.displayMetrics.density
    private fun Float.spToPx(): Float = this * resources.displayMetrics.scaledDensity
}